<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "video_type".
 *
 * @property integer $id
 * @property string $name
 * @property integer $level
 * @property integer $pid
 */
class VideoType extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'video_type';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid'], 'integer'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'pid' => 'Pid',
        ];
    }

    //无限极分类
    public static function getHighestLevel($data,$pid = 0,$lev = 1)
    {
//        var_dump($data);die;
        $tree  = [];
        if ($data && is_array($data)) {
            foreach ($data as $k => $v) {
                if ($v["pid"] == $pid) {
                    $v["name"] = str_repeat('——',$lev)."  ".$v['name'];
                    $tree[] = $v;
                    $tree = array_merge($tree,self::getHighestLevel($data,$v['id'],$lev+1));
                }
            }
        }
        return $tree;
    }
}
